# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do

  desc "Upload APK to Play Store"
  lane :deploy_playstore do

    props = property_file_read(file: "app/version/version.properties")
    version = props["VERSION"]
    apkPath = "app/build/outputs/apk/release/duckduckgo-#{version}-release.apk"

    upload_to_play_store(
      apk: apkPath,
      track: 'internal'
    )
  end

  desc "Deploy APK to GitHub"
  lane :deploy_github do

    props = property_file_read(file: "app/version/version.properties")
    version = props["VERSION"]
    releaseNotes = File.read("../app/version/release-notes")
    apkPath = "app/build/outputs/apk/release/duckduckgo-#{version}-release.apk"
    token = ENV["GITHUB_UPLOAD_TOKEN"]

    UI.message ("Upload new app version to GitHub\nVersion: #{version}\nRelease Notes:\n=====\n#{releaseNotes}\n=====\n")

    set_github_release(
        repository_name: "DuckDuckGo/Android",
        api_token: token,
        name: version,
        tag_name: version,
        description: releaseNotes,
        upload_assets: [apkPath],
        is_draft: false,
        is_prerelease: false)

    end

    # Note, this currently relies on having `git flow` tools installed.
    # This dependency could be removed with a little more time to tidy up this script to do the branching/merging manually.

    desc "Create new release"
      lane :release do

        UI.header "Creating a new release"
        UI.message ("Checking current branch: #{git_branch}")

        ensure_git_status_clean

        ensure_git_branch(
          branch: 'develop'
        )

        newVersion = prompt(text: "\nLast release was: #{last_git_tag}\nEnter New Version Number:")
        releaseNotes = prompt(text: "Release Notes: ", multi_line_end_keyword: "END")
        commits = changelog_from_git_commits(
            between: [last_git_tag, "HEAD"],
            pretty: "- %s",
            date_format: "short",
            match_lightweight_tag: false,
            merge_commit_filtering: "exclude_merges"
        )
        UI.message ("Performing a new release for #{newVersion}")

        if UI.confirm("Are you sure you're happy with this release?\n\nVersion=#{newVersion}\nCommits Since Last Release:\n#{commits}\nRelease Notes:\n#{releaseNotes}\n")
          UI.success "Creating release branch for release/#{newVersion}"

          sh "git flow release start #{newVersion}"

          File.open('../app/version/version.properties', 'w') do |file|
            file.write("VERSION=#{newVersion}")
          end

          File.open('../app/version/release-notes', 'w') do |file|
            file.write("""## What's new in this release?
#{releaseNotes}

## Have feedback?
You can always reach us at https://duckduckgo.com/feedback.""")
          end

          if UI.confirm(text:"If you have any other changes to make to the release branch, do them now. Enter `y` when ready to create and push tags")

            git_commit(path: "*", message: "Updated release notes and version number for new release - #{newVersion}")
            
            sh "git flow release finish -mnFS '#{newVersion}' '#{newVersion}'"
            push_git_tags
            sh "git push origin master"
            sh "git push origin develop"

            UI.header("#{newVersion} has been successfully released 🎉")

          else
            UI.error "Release cancelled 😢"
          end

        else
          UI.error "Release cancelled 😢"
        end

        end

end